package com.pencilisland.pay.mgt.console.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 登录处理器
 * 
 * @author Breeze
 *
 */
@Controller
@RequestMapping(value = "/console/login")
public class LoginController {

	Logger log = LoggerFactory.getLogger(getClass());

	/**
	 * 进入登录页面
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String toLogin(HttpServletRequest request, HttpServletResponse response) {

		if (SecurityUtils.getSubject().getSession() != null) {
			SecurityUtils.getSubject().logout();
		}

		Boolean isAjax = false;

		Enumeration<String> values = request.getHeaders("X-Requested-With");
		while (values.hasMoreElements()) {
			String value = values.nextElement();
			if ("XMLHttpRequest".equalsIgnoreCase(value)) {
				isAjax = true;
				break;
			}
		}

		log.debug("登录页面,ajax[{}]>>", isAjax);

		if (isAjax) {
			response.setHeader("sessionStatus", "timeout");
		}

		return "console/login";
	}

	/**
	 * 登录失败
	 * 
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	public String fail() {
		log.debug("登录失败>>");

		return "console/login";
	}
}
